package com.zhkj.crawl.controller;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class BaseController {

    @ExceptionHandler({ Exception.class })
    @ResponseBody
    public Ret handleIndexOutOfBoundsException(Exception e) {
        log.error("接口错误", e);
        return Ret.fail(e.getMessage());
    }

    @Data
    @AllArgsConstructor
    public static class Ret{
        private int ack;
        private String msg;
        private Object data;

        public static Ret fail(String msg){
            return new Ret(0, msg, null);
        }

        public static Ret success(){
            return new Ret(1, "成功", null);
        }

        public static Ret success(Object data){
            return new Ret(1, "成功", data);
        }
    }
}
